import axios from 'axios';
import qs from 'qs'; 

let base = '';

//添加一个请求拦截器
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    // var user = sessionStorage.getItem('user');
    // var sessionName = "JSESSIONID";
    // var sessionTmpVal = window.document.cookie.match('(^|;) ?' + sessionName + '=([^;]*)(;|$)');
    // var currentSessionId = sessionTmpVal ? sessionTmpVal[2] : null;

    // console.log(user);
    // if (user) {
    //     user = JSON.parse(user);
    //     //从本地COOKIE中获取JSESSIONID值
    //     console.log("user.sessionid:" + user.sessionid);
    //     console.log("currentSessionId:" + currentSessionId);
    //     if (user.sessionid != currentSessionId) {
    //         //清除token信息并跳转到登录页面
    //         sessionStorage.removeItem('user');
    //         //提示您已经超时，请重新登录
    //         location.href = "/#/login";
    //     }
    // }


    var shopToken = localStorage.getItem("shopToken");
	if(shopToken != null){
		config.headers['S-Token'] = shopToken;
    }
    var userToken = sessionStorage.getItem("userToken");
	if(userToken != null){
		config.headers['U-Token'] = userToken;
	}
	// if(config.method=='post'){
	// 	config.data = {
	// 		...config.data,
	// 		_t: Date.parse(new Date())/1000,
	// 	}
	// }else if(config.method=='get'){
	// 	config.params = {
	// 		_t: Date.parse(new Date())/1000,
	// 		...config.params
	// 	}
	// }
    return config;
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

//http response 拦截器
axios.interceptors.response.use(
    response => {
        const data = response.data;
        switch (data.code) {
            case '1003':
                localStorage.removeItem("shopToken");
                sessionStorage.removeItem('userToken');
                location.href = "/#/login";
                break;
            case 'SHIRO_E5001':
                break;
            default:
        }
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    //返回401,清除token信息并跳转到登录页面
                    sessionStorage.removeItem('user');
                    //提示您已经超时，请重新登录
                    location.href = "/#/login";
            }
        }
        return Promise.reject(error.response.data); //返回接口返回的错误信息
    });




//获取服务器端登陆验证码的配置状态信息
export const getLoginValidcodeStatusinfo = params => { return axios.get(`${base}/api/user/login/validcode/status/info`, { params: params }).then(res => res.data); };


//店铺用户登录
export const requestLogin = params => { return axios.post(`${base}/api/v1/user/login`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//店铺员工列表
export const getShopStaffs = params => { return axios.post(`${base}/api/v1/staff/all`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//查看员工详细信息
export const getStaffDetail = params => { return axios.get(`${base}/api/v1/staff/detail`, { params: params }).then(res => res.data); };
//店铺获取经理信息
export const getShopManager = params => { return axios.post(`${base}/api/v1/staff/manager`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//员工登录
export const staffLogin = params => { return axios.post(`${base}/api/v1/staff/login`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//员工重置密码
export const resetPwd = params => { return axios.post(`${base}/api/v1/staff/pwd/reset`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//删除员工
export const delStaff = params => { return axios.get(`${base}/api/v1/staff/del`, { params: params }).then(res => res.data); };
//增加员工
export const addStaff = params => { return axios.post(`${base}/api/v1/staff/add`, params).then(res => res.data); };
//修改员工
export const editStaff = params => { return axios.post(`${base}/api/v1/staff/edit`, params).then(res => res.data); };


//店铺预约列表
export const getShopAppointments = params => { return axios.post(`${base}/api/v1/appointment/all`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//删除预约
export const delAppointment = params => { return axios.get(`${base}/api/v1/appointment/del`, { params: params }).then(res => res.data); };
//增加预约
export const addAppointment = params => { return axios.post(`${base}/api/v1/appointment/add`, params).then(res => res.data); };
//修改预约
export const editAppointment = params => { return axios.post(`${base}/api/v1/appointment/edit`, params).then(res => res.data); };
//修改预约到店
export const checkinAppointment = params => { return axios.get(`${base}/api/v1/appointment/checkin`, { params: params }).then(res => res.data); };
//查询某个预约信息
export const getAppointment = params => { return axios.get(`${base}/api/v1/appointment/get`, { params: params }).then(res => res.data); };
//今日销售列表
export const getTodaySales = params => { return axios.post(`${base}/api/v1/order/today`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//获取某一天的订单列表
export const getOnedaySales = params => { return axios.post(`${base}/api/v1/order/oneday`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//获取今日店铺或者员工的打印报表信息
export const printTodaySale = params => { return axios.get(`${base}/api/v1/order/today/report/print`, { params: params }).then(res => res.data); };

//查询店铺的所有服务项目列表
export const getProjects = params => { return axios.get(`${base}/api/v1/project/item/all`, { params: params }).then(res => res.data); };
//添加服务项目
export const addProject = params => { return axios.post(`${base}/api/v1/project/item/add`, params).then(res => res.data); };
//修改服务项目
export const editProject = params => { return axios.post(`${base}/api/v1/project/item/edit`, params).then(res => res.data); };
//删除服务项目
export const delProject = params => { return axios.get(`${base}/api/v1/project/item/del`, { params: params }).then(res => res.data); };

//添加订单(结账)
export const addOrder = params => { return axios.post(`${base}/api/v1/order/add`, params).then(res => res.data); };
//编辑订单小费(今日销售)
export const editOrderTip = params => { return axios.get(`${base}/api/v1/order/edit/tip`, { params: params }).then(res => res.data); };
//批量编辑订单小费(今日销售)
export const batchEditOrderTip = params => { return axios.post(`${base}/api/v1/order/batchedit/tip`,  params).then(res => res.data); };
//删除订单(今日销售)
export const delOrder = params => { return axios.get(`${base}/api/v1/order/del`, { params: params }).then(res => res.data); };




//######报表######
export const getShopDateSaleReport = params => { return axios.post(`${base}/api/v1/report/shop/date/sale/record`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
export const getShopDateSaleSum = params => { return axios.post(`${base}/api/v1/report/shop/date/sale/sum`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//统计店铺当日的所有员工薪水
export const statShopStaffSalary = params => { return axios.get(`${base}/api/v1/report/shop/date/salary`, { params: params }).then(res => res.data); };
export const queryStaffDaysSalePrint = params => { return axios.get(`${base}/api/v1/report/staff/days/sale/print`, { params: params }).then(res => res.data); };
export const queryShopDaysSalePrint = params => { return axios.get(`${base}/api/v1/report/shop/days/sale/print`, { params: params }).then(res => res.data); };




//删除店铺的历史订单数据
export const clearShopHistory = params => { return axios.get(`${base}/api/v1/report/clear`, { params: params }).then(res => res.data); };

//######礼卡######

//分页查询店铺的所有礼卡列表
export const getShopGiftcards = params => { return axios.post(`${base}/api/v1/giftcard/all`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//根据关键词模糊查询对应的礼卡列表
export const likeShopGiftCards = params => { return axios.get(`${base}/api/v1/giftcard/like`, { params: params }).then(res => res.data); };


//删除指定的礼卡
export const delGiftcard = params => { return axios.get(`${base}/api/v1/giftcard/del`, { params: params }).then(res => res.data); };
//查询指定店铺的礼卡总余额
export const getShopGiftcardBalance = params => { return axios.get(`${base}/api/v1/giftcard/balance`, { params: params }).then(res => res.data); };
//根据礼卡编号查询指定的礼卡的详细信息
export const getGiftcardDetail = params => { return axios.get(`${base}/api/v1/giftcard/detail/cardId`, { params: params }).then(res => res.data); };
//根据礼卡号码查询指定的礼卡的详细信息
export const getGiftcardDetailByCardno = params => { return axios.get(`${base}/api/v1/giftcard/detail/cardNo`, { params: params }).then(res => res.data); };
//根据礼卡编号查询其所有的消费记录列表
export const getGiftCardPayRecords = params => { return axios.post(`${base}/api/v1/giftcard/consume/records`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };
//增加礼卡
export const addGiftcard = params => { return axios.post(`${base}/api/v1/giftcard/add`, params).then(res => res.data); };
//修改礼卡
export const editGiftcard = params => { return axios.post(`${base}/api/v1/giftcard/edit`, params).then(res => res.data); };
//补卡
export const repairGiftcard = params => { return axios.post(`${base}/api/v1/giftcard/repair`, qs.stringify(params),{
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(res => res.data); };

//######系统设置######

//查询店铺详细信息
export const getShopDetail = params => { return axios.get(`${base}/api/v1/shop/detail`, { params: params }).then(res => res.data); };
//保存店铺信息
export const saveShopInfo = params => { return axios.post(`${base}/api/v1/shop/save`, params).then(res => res.data); };
